package com.onadake.domain;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import com.onadake.utils.ValidationUtils;

/**
 * Data block object. Every block is a package of data within entity object.
 * 
 * @author radek.hecl
 *
 */
public class Data {

    /**
     * Builder object.
     */
    public static class Builder {

        /**
         * Code of the owner entity.
         */
        private String entityCode;

        /**
         * Name of the block.
         */
        private String binaryName;

        /**
         * Sets code of the owner entity.
         * 
         * @param entiyCode code of the owner entity
         * @return this instance
         */
        public Builder setEntityCode(String entiyCode) {
            this.entityCode = entiyCode;
            return this;
        }

        /**
         * Sets name of the data block.
         * 
         * @param binaryName name of the data block
         * @return this instance
         */
        public Builder setBinaryName(String binaryName) {
            this.binaryName = binaryName;
            return this;
        }

        /**
         * Builds the result object.
         * 
         * @return created object
         */
        public Data build() {
            return new Data(this);
        }

    }

    /**
     * Code of the owner entity.
     */
    private String entityCode;

    /**
     * Name of the block.
     */
    private String binaryName;

    /**
     * Creates new instance.
     * 
     * @param builder builder object
     */
    public Data(Builder builder) {
        entityCode = builder.entityCode;
        binaryName = builder.binaryName;
        guardInvariants();
    }

    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
        ValidationUtils.guardMatch(entityCode, "[A-Z]{2}[0-9]{16}", "entityCode must match pattern");
        ValidationUtils.guardNotEmpty(binaryName, "binaryName cannot be empty");
    }

    /**
     * Returns code of the entity which owns this data block.
     * 
     * @return code of the entity which owns this data block
     */
    public String getEntityCode() {
        return entityCode;
    }

    /**
     * Returns name of this data block.
     * 
     * @return name of this data block
     */
    public String getBinaryName() {
        return binaryName;
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}
